// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2

package tenant

import (
	"context"

	"nexus/internal/svc"
	"nexus/internal/types"
	"nexus/model"
	"nexus/referenceImpl/tenant_reference_impl"
	"nexus/utils/errorx"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type CreateTenantLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateTenantLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateTenantLogic {
	return &CreateTenantLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateTenantLogic) CreateTenant(req *types.CreateTenantReq) (resp *types.BaseRespWithData, err error) {
	// 开启事务
	tx := model.NewTXGenerator((*gorm.DB)(l.svcCtx.NexusDB)).CreateTX(l.ctx)

	var data interface{}
	err = tx.AutoCommit(func() error {
		// 执行业务
		data, err = tenant_reference_impl.NewTenantReferenceImpl(l.svcCtx).CreateTenant(l.ctx, req)
		return err
	})

	return errorx.BaseResponseWithData(err, data)
}
